﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockMatch.WebSite
{
    public abstract class WebViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel>
    {
        protected WebViewPage()
            : base()
        {

        }

        protected dynamic Title
        {
            get { return ViewData["Title"]; }
            set { ViewData["Title"] = value; }
        }
        protected dynamic KeyWords
        {
            get { return ViewData["KeyWords"]; }
            set { ViewData["KeyWords"] = value; }
        }
        protected dynamic Description
        {
            get { return ViewData["Description"]; }
            set { ViewData["Description"] = value; }
        }

        /// <summary>
        /// 注册的脚本
        /// </summary>
        public List<string> RegisteredScripts
        {
            get
            {
                if (ViewBag._registeredScripts == null)
                {
                    ViewBag._registeredScripts = new List<string>();
                }
                return (List<string>)ViewBag._registeredScripts;
            }
        }
        /// <summary>
        /// 注册一个脚本引用
        /// </summary>
        /// <param name="scriptUrl"></param>
        public void RegistScript(string scriptUrl)
        {
            if (string.IsNullOrWhiteSpace(scriptUrl))
            {
                throw new ArgumentNullException("scriptUrl");
            }

            scriptUrl = scriptUrl.Trim().ToLower() + "?t=" + GlobalConfig.Version;
            if (!RegisteredScripts.Contains(scriptUrl))
            {
                RegisteredScripts.Add(scriptUrl);
            }
        }
        /// <summary>
        /// 注册一个脚本引用
        /// </summary>
        /// <param name="scriptUrl">脚本地址</param>
        /// <param name="index">引用顺序</param>
        public void RegistScript(string scriptUrl, int index)
        {
            if (string.IsNullOrWhiteSpace(scriptUrl))
            {
                throw new ArgumentNullException("scriptUrl");
            }
            scriptUrl = scriptUrl.Trim().ToLower() + "?t=" + GlobalConfig.Version;
            if (!RegisteredScripts.Contains(scriptUrl))
            {
                if (index < RegisteredScripts.Count)
                {
                    RegisteredScripts.Insert(index, scriptUrl);
                }
                else
                {
                    RegisteredScripts.Add(scriptUrl);
                }

            }
        }


        /// <summary>
        /// 注册的css
        /// </summary>
        public List<string> RegisteredCss
        {
            get
            {
                if (ViewBag._registeredCss == null)
                {
                    ViewBag._registeredCss = new List<string>();
                }
                return (List<string>)ViewBag._registeredCss;
            }
        }
        /// <summary>
        /// 注册一个css文件
        /// </summary>
        /// <param name="css"></param>
        public void RegistCss(string css)
        {
            if (string.IsNullOrWhiteSpace(css))
            {
                throw new ArgumentNullException("css");
            }
            css = css.Trim().ToLower() + "?t=" + GlobalConfig.Version;
            if (!RegisteredCss.Contains(css))
            {
                RegisteredCss.Add(css);
            }
        }
        /// <summary>
        /// 注册一个css文件
        /// </summary>
        /// <param name="css"></param>
        /// <param name="index">引用顺序</param>
        public void RegistCss(string css, int index)
        {
            if (string.IsNullOrWhiteSpace(css))
            {
                throw new ArgumentNullException("css");
            }
            css = css.Trim().ToLower() + "?t=" + GlobalConfig.Version;
            if (!RegisteredScripts.Contains(css))
            {
                if (index < RegisteredCss.Count)
                {
                    RegisteredCss.Insert(index, css);
                }
                else
                {
                    RegisteredCss.Add(css);
                }

            }
        }

    }
}
